<?php
namespace Modules\Payment\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Order\Entities\UnionOrder;
use \Modules\Order\Entities\BaseOrder as Order;

use Validator;
use ThrowException;
use Payment;
use Cache;
use Miniapp;

class KuaishouCallbackController extends Controller
{
    // 快手支付回调
    public function pay(Request $req) 
    {

        $pay = Payment::kuaishouPay('default', 'wechat');

        if (!$pay->checkCallbackData($req)) {
            ThrowException::Conflict('Forbidden');
        }

        $orderNumber = explode('_', @$req->input('data')['out_order_no'])[0];
        $status = @$req->input('data')['status'];

        $order = Order::getDerivedOrder([
            'number' => $orderNumber
        ]);

        // 支付成功
        if ($status == 'SUCCESS') {

            $options = $req->all();

            $options['pay_type'] = 'kuaishou';
            $options['pay_sub_type'] = strtolower(@$req->input('data')['channel'] ?: '');

            // 完成订单
            $order->completePay($options);

            // \Log::error($options);

            return [
                "result"  => 1,
                "message_id"  => $req->input('message_id')
            ];
        }

        return [];
    }

}
